package com.qf.web;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Dept;
import com.qf.bean.Menu;
import com.qf.bean.Role;
import com.qf.service.DeptService;
import com.qf.service.MenuService;
import com.qf.service.RoleService;
import com.qf.utils.filter.ExceUtils;
import com.qf.utils.filter.enuml.PageEnum;
import com.qf.utils.filter.enuml.RoleEnum;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.activiti.engine.impl.util.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * IntelliJ IDEA -> person
 * 2019/11/20 21:20
 *
 * @author zmyao
 * 别看，我是渣渣。
 */
@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private MenuService menuService;

/*    @RequestMapping("/resource/demo3/deleteByRoleidsA")
    @ResponseBody
    public Map<String,String> deleteByRoleidsA(String ids){
        JSONArray jsonArray = new JSONArray(ids);
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            list.add(jsonArray.optInt(i));
        }
        System.out.println(list);
        return null;
    }*/

    @RequestMapping("/resource/demo2/getallroles")
    @ResponseBody
    public List<Role> getallrolesByDid(int deptid) {
        List<Role> roles = roleService.getallrolesByDid(deptid);
        return roles;
    }

    @RequestMapping("/resource/demo3/findAllRoles")
    public String findAllRoles(ModelMap map, @RequestParam(defaultValue = "1") int index, String rname, String deptname, Integer state) {
        System.out.println("state:"+state);
        PageInfo<Role> pageInfo = roleService.findAllRoles(index, PageEnum.PAGESIZE.getSize(), rname, deptname, state);
        map.addAttribute("pageInfo", pageInfo);
        map.addAttribute("rname", rname);
        map.addAttribute("deptname", deptname);
        map.addAttribute("state", state);
        for (Role role : pageInfo.getList()) {
            System.out.println("role"+role);
        }
        return "resource/demo3/list";
    }

    @RequestMapping("/resource/demo3/getDeptAndMenu")
    public String getDeptAndMenu(ModelMap map) {
        List<Dept> depts = deptService.findalldepts();
        List<Menu> menus = menuService.findAllMenus();
        map.addAttribute("depts", depts);
        map.addAttribute("menus", menus);
        return "/resource/demo3/add";
    }


    @RequestMapping("/resource/demo3/addRole")
    public void addRole(Role role, int[] menuids, HttpServletResponse response) {
        try {
            int len = roleService.insertRole(role, menuids);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (len > 0) {
                out.print("<script>alert('添加成功!');location.href='/resource/demo3/findAllRoles';</script>");
            } else {
                out.print("<script>alert('添加失败!');location.href='/resource/demo3/getDeptAndMenu';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/resource/demo3/findRoleById")
    public String findRoleById(Integer roleid, ModelMap map) {
        Role role = roleService.findRoleAndMenuById(roleid);
        map.addAttribute("role", role);
        List<Dept> depts = deptService.findalldepts();
        List<Menu> menus = menuService.findAllMenus();
        map.addAttribute("depts", depts);
        map.addAttribute("menus", menus);
        return "/resource/demo3/update";
    }

    @RequestMapping("/resource/demo3/updateRole")
//    public void updateRole(Role role,@RequestParam(value = "beforeMenuIdsList") List<Integer> beforeMenuIdsList,int[] menuids,HttpServletResponse response){
    public void updateRole(Role role, int[] menuids, HttpServletResponse response) {
        try {
            int len = roleService.updateRole(role, menuids);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (len > 0) {
                out.print("<script>alert('修改成功!');location.href='/resource/demo3/findAllRoles';</script>");
            } else {
                out.print("<script>alert('修改失败!');location.href='/resource/demo3/findRoleById?roleid=" + role.getRoleid() + "';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/resource/demo3/deleteRole")
    public void deleteRoleById(Integer roleid, HttpServletResponse response) {
        try {
            int len = roleService.deleteRoleById(roleid);
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (len == -2) {
                out.print("<script>alert('该角色不可以删除，有用户使用着!');location.href='/resource/demo3/findAllRoles';</script>");
            } else if (len > 0) {
                out.print("<script>alert('删除成功!');location.href='/resource/demo3/findAllRoles';</script>");
            } else {
                out.print("<script>alert('删除失败!');location.href='/resource/demo3/findAllRoles';</script>");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/resource/demo3/deleteByRoleids")
    public void deleteByRoleids(int[] roleids, HttpServletResponse response,String method) {
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            if (roleids == null){
                out.print("<script>alert('请您选择想要删除的选项!');location.href='/resource/demo3/findAllRoles';</script>");
            }
            if("delete".equals(method)){
                Map<String, Object> map = roleService.setRoleStateDelete(roleids, RoleEnum.DELETE.getState());
                System.out.println("map:"+map);
                List<String> rnamelList = (List<String>) map.get("rnamelList");

                int deleteLen = (int) map.get("deleteLen");

                if (rnamelList != null  ) {
                   if (deleteLen == -2) {
                        out.print("<script>alert('你选择的全部的角色被用户使用,都不能删除!');location.href='/resource/demo3/findAllRoles';</script>");
                        return;
                    }
                    out.print("<script>alert('" + rnamelList.toString() + "这些角色被用户使用不能删除!');location.href='/resource/demo3/findAllRoles';</script>");
                } else if (deleteLen > 0) {
                    out.print("<script>alert('全部删除成功!');location.href='/resource/demo3/findAllRoles';</script>");
                }  else {
                    out.print("<script>alert('删除失败!');location.href='/resource/demo3/findAllRoles';</script>");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
